<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use app\service\propertyService;
use app\service\propertyZftService;
use think\Db;
class PropertyZft extends Common{
    public function getList(){
        //查询服务商数据条数
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);
        //查询服务商信息
        $data = request()->get();

        $where = array_filter($where);
        $prolist = (new propertyZftService())->getList($where);
        $number = count((new propertyZftService())->zftsetNumber($where));
        $this->assign('number',$number);
        if(!empty($number)){
            foreach($prolist as $lis){
                $id = $lis['property_id'];
                $proname = (new propertyService())->propertyId($id);
                $lis['property_name'] = $proname['name'];
                $list[] = $lis;
                $this->assign('list',$list);
            }
        }else{
            $this->assign('list',$prolist);
        }
        $this->assign('prolist',$prolist);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }
        
        return $this->fetch('propertyzft/zft_list');
    }

    //添加
    public function add(){
        //查询服务商信息
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);
        $propertylist =(new communityService())->propertyList($where);
        $this->assign('propertylist',$propertylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();

            //判断账号名称是否重复        
            $info = Db::table("property_zftset")->where("property_id",$data["property_id"])->find();
            if($info){
                $this->error('该物业配置已添加已存在');
            }

            //插入数据到数据库
            $result = (new propertyZftService())->add($data);
            if($result){
                return "支付通配置添加成功";
                $this->success('添加物业成功',url('propertyList'));
            }else{
                $this->error('添加物业失败',url('propertyAdd'));
            }
        }
        return $this->fetch('propertyzft/add');
    }
    //修改
    public function edit(){
        //查询服务商信息

        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('物业不存在');
            }
            $info=Db::table("property_zftset")->where("id",$id)->find();
            $propertyinfo = (new propertyService())->propertyId($info["property_id"]);

            $this->assign('propertyinfo',$propertyinfo);
            $this->assign('info',$info);
            return $this->fetch('propertyzft/edit');
        }else{
            $data = request()->post();
             dump($data);exit();
            $result = (new propertyService())->propertyUpdate($data);
            if($result){
                return "添加物业成功";
                $this->success('修改物业成功',url('propertyList'));
            }else{
                $this->error('修改物业失败');
            }
        }
        return $this->fetch('edit');
    }
    //删除
    public function zftDel(){
        $id = request()->param('id');
		$validate = $this->validate(request()->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new propertyService())->propertyId($id);
		if(empty($info)){
			$this->error('该物业不存在');
		}
		$result = Db::name('property')->delete($id);
        if($result){
            $this->success('删除物业成功',url('propertyList'));
		}else{
            $this->error('删除物业失败',url('propertyList'));
		}
    }

}